package com.wulang.extension.config;

import com.wulang.extension.constants.Constants;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {
    @Bean("ackQueue")
    public Queue ackQueue() {
        return QueueBuilder.durable(Constants.ACK_QUEUE).build();
    }

    @Bean("ackExchange")
    public Exchange ackExchange() {
        //默认是持久化的交换机
        return ExchangeBuilder.directExchange(Constants.ACK_EXCHANGE).build();
    }

    @Bean("ackBinding")
    public Binding ackBinding(@Qualifier("ackExchange") Exchange exchange, @Qualifier("ackQueue") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("ack").noargs();
    }

    @Bean("persQueue")
    public Queue persQueue() {
        return QueueBuilder.durable(Constants.PERS_QUEUE).build();
    }

    @Bean("persExchange")
    public Exchange persExchange() {
        return ExchangeBuilder.directExchange(Constants.PERS_EXCHANGE).durable(false).build();
    }

    @Bean("persBinding")
    public Binding persBinding(@Qualifier("persExchange") Exchange exchange, @Qualifier("persQueue") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("pers").noargs();
    }

    @Bean("confirmQueue")
    public Queue confirmQueue() {
        return QueueBuilder.durable(Constants.COFIRM_QUEUE).build();
    }

    @Bean("confirmExchange")
    public Exchange confirmExchange() {
        return ExchangeBuilder.directExchange(Constants.CONFIRM_EXCHANGE).build();
    }

    @Bean
    public Binding confirmBinding(@Qualifier("confirmQueue") Queue queue, @Qualifier("confirmExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("confirm").noargs();
    }


    @Bean("retryQueue")
    public Queue retryQueue() {
        return QueueBuilder.durable(Constants.RETRY_QUEUE).build();
    }

    @Bean("retryExchange")
    public Exchange retryExchange() {
        return ExchangeBuilder.directExchange(Constants.RETRY_EXCHANGE).build();
    }

    @Bean("retryBinding")
    public Binding retryBinding(@Qualifier("retryQueue") Queue queue, @Qualifier("retryExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("retry").noargs();
    }


    @Bean("ttlQueue")
    public Queue ttlQueue() {
        return QueueBuilder.durable(Constants.TTL_QUEUE).build();
    }

    @Bean("ttlQueue2")
    public Queue ttlQueue2() {
        return QueueBuilder.durable(Constants.TTL_QUEUE2).ttl(50000).build();//队列5s过期
    }

    @Bean("ttlExchange")
    public Exchange ttlExchange() {
        return ExchangeBuilder.directExchange(Constants.TTL_EXCHANGE).build();
    }

    @Bean("ttlBinding")
    public Binding ttlBinding(@Qualifier("ttlExchange") Exchange exchange, @Qualifier("ttlQueue") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("ttl").noargs();
    }

    @Bean("ttlBinding2")
    public Binding ttlBinding2(@Qualifier("ttlExchange") Exchange exchange, @Qualifier("ttlQueue2") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("ttl").noargs();
    }
}
